package com.cmc.cloud.cmclink.doc.controller.admin.multimodaltransport;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.multimodaltransport.ContainerManifestService;
import com.cmc.cloud.cmclink.doc.util.FileOperateUtil;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest.ContainerManifestImportAllReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest.ContainerManifestImportReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest.ContainerManifestPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest.ContainerManifestRespVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 集装箱清单-出口
 *
 * @author dev89
 * @date 2025/10/17 13:16
 */
@Tag(description = "后台管理-集装箱清单-出口", name = "后台管理-集装箱清单-出口")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL + "/containerManifest/EX")
public class ContainerManifestController {

    @Resource
    private ContainerManifestService containerManifestService;

    @OperateLog(type = OperateTypeEnum.IMPORT)
    @Operation(summary = "导入集装箱清单")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPermission('doc:containerlist:import')")
    public CommonResult<Void>
        importContainerManifest(@Valid @RequestBody ContainerManifestImportAllReqVO containerManifestImportAllReqVO) {
        containerManifestService.importContainerManifest(containerManifestImportAllReqVO, ExImTypeEnum.EX.getValue());
        return CommonResult.success();
    }

    @OperateLog(type = OperateTypeEnum.OTHER)
    @Operation(summary = "校验集装箱清单")
    @PostMapping("/check")
    public CommonResult<List<ContainerManifestImportReqVO>>
        checkContainerManifest(@Valid @RequestBody ContainerManifestImportAllReqVO containerManifestImportAllReqVO) {
        return CommonResult.success(containerManifestService.checkContainerManifest(containerManifestImportAllReqVO,
            ExImTypeEnum.EX.getValue()));
    }

    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PostMapping("/excel-template")
    @Operation(summary = "下载excel导入模板")
    @PreAuthorize("@ss.hasPermission('doc:containerlist:download')")
    public CommonResult<Void> downloadExcelTemplateImport(HttpServletResponse response) {
        String fileName = "containerEX.xlsx";
        String templatePath = "templates/containerEX.xlsx";
        return FileOperateUtil.downloadExcelTemplate(response, fileName, templatePath);
    }

    @OperateLog(type = OperateTypeEnum.GET)
    @Operation(summary = "获得集装箱清单分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:containerlist:search')")
    public CommonResult<PageResult<ContainerManifestRespVO>>
        getContainerManifestPage(@Valid @RequestBody ContainerManifestPageReqVO pageVo) {
        PageResult<ContainerManifestRespVO> pageResult =
            containerManifestService.getContainerManifestPage(pageVo, ExImTypeEnum.EX.getValue());
        return CommonResult.success(pageResult);
    }
}
